package com.tcm.controller.backend;

import com.tcm.common.enums.ResponseEnum;
import com.tcm.entity.dto.param.ClazzPageParam;
import com.tcm.entity.dto.ClazzUpdateDto;
import com.tcm.entity.resp.PageR;
import com.tcm.entity.resp.R;
import com.tcm.entity.vo.*;
import com.tcm.exception.EntityInvalidException;
import com.tcm.service.ClazzService;
import com.tcm.utils.AccessTokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * 班级管理控制类
 * @author Guqier
 * @version 1.0
 * @date 2023/7/5 20:48
 */
@Slf4j
@RestController
@RequestMapping("/pc/clazz")
public class ClazzController {

    @Autowired
    private ClazzService clazzService;

    /**
     * 添加班级信息
     * @param clazzInfo 班级更新信息封装对象
     * @param request http请求对象
     * @return 响应结果
     */
    @RequestMapping(method = RequestMethod.POST)
    public R addClazzInfo(@RequestBody @Valid ClazzUpdateDto clazzInfo, HttpServletRequest request){
        boolean res = false;
        try {
            res = clazzService.addClazzInfo(clazzInfo, AccessTokenUtil.getUserId(request));
        } catch (EntityInvalidException e) {
            return R.fail(101, e.getMessage());
        }
        return res ? R.success() : R.fail(ResponseEnum.DATABASE_OPERATION_FAIL);
    }

    /**
     * 分页获取班级列表
     * @param clazzPageParam 获取班级分页数据参数封装类对象
     * @return 响应结果
     */
    @RequestMapping(method = RequestMethod.GET)
    public R getClazzListByPage(ClazzPageParam clazzPageParam){
        PageR<ClazzListResult> res = clazzService.getClazzListByPage(clazzPageParam);
        return R.success(res);
    }

    /**
     * 修改班级信息
     * @param clazzInfo 班级信息更新数据对象
     * @param request http请求对象
     * @return 响应结果
     */
    @RequestMapping(method = RequestMethod.PUT)
    public R modifyClazzInfo(@RequestBody @Valid ClazzUpdateDto clazzInfo, HttpServletRequest request){
        if (clazzInfo.getId() == null) {
            return R.fail(ResponseEnum.PARAM_IS_BLANK);
        }
        boolean res = false;
        try {
            res = clazzService.modifyClazzInfo(clazzInfo, AccessTokenUtil.getUserId(request));
        } catch (EntityInvalidException e) {
            return R.fail(101, e.getMessage());
        }
        return res ? R.success() : R.fail(ResponseEnum.DATABASE_OPERATION_FAIL);
    }

    /**
     * 删除班级信息
     * @param ids 班级ID集合
     * @param request http请求对象
     * @return 响应结果
     */
    @RequestMapping(method = RequestMethod.DELETE)
    public R removeClazzInfo(@RequestParam("ids") List<String> ids, HttpServletRequest request){
        if (ids.isEmpty()) {
            return R.fail(ResponseEnum.PARAM_IS_BLANK);
        }
        boolean res = clazzService.removeClazzInfo(ids, AccessTokenUtil.getUserId(request));
        return res ? R.success() : new R(ResponseEnum.DATABASE_OPERATION_FAIL);
    }

    /**
     * 通过当前登录教师用户获取所教班级列表
     * @param request http请求对象
     * @return 所教班级列表
     */
    @RequestMapping("/byTeacher")
    public R getClazzListAccordingToTeacher(HttpServletRequest request){
        List<ClazzSelectResult> clazzListAccordingToTeacher = clazzService.getClazzListAccordingToTeacher(AccessTokenUtil.getUserId(request));
        if (clazzListAccordingToTeacher == null){
            return R.fail(0, "当前登录非教师用户");
        }
        return R.success(clazzListAccordingToTeacher);
    }
}
